package io.sk.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.sk.common.base.Constants;
import io.sk.common.utils.CheckDataUtils;
import io.sk.common.utils.ResourceUtils;
import io.sk.dao.ClassDao;
import io.sk.dao.ClassTeacherDao;
import io.sk.dao.PersonnelDao;
import io.sk.entity.ClassEntity;
import io.sk.entity.ClassTeacherEntity;
import io.sk.entity.PersonnelEntity;
import io.sk.service.ClassTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 班级教师关系表
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/5/8 16:39
 */
@Service("classTeacherService")
public class ClassTeacherServiceImpl extends ServiceImpl<ClassTeacherDao, ClassTeacherEntity> implements ClassTeacherService {

    @Autowired
    private ClassTeacherDao classTeacherDao;

    @Autowired
    private PersonnelDao personnelDao;

    @Autowired
    private ClassDao classDao;

    /**
     * 新建人员班级关系信息
     *
     * @param classId             班级id
     * @param personnelId         人员id
     * @param personnelType       人员类型：1、班主任，2、任课老师:默认为班主任
     * @param teacherIntroduction 教师个人介绍
     * @return
     */
    @Override
    public boolean save(String classId, String personnelId, Integer personnelType, String teacherIntroduction) {

        //数据验证
        if (CheckDataUtils.isAnyEmptyString(classId, personnelId)) {
            return false;
        }

        //人员id验证
        PersonnelEntity personnelEntity = personnelDao.selectById(personnelId.trim());
        if (null == personnelEntity) {
            return false;
        }

        //班级id验证
        ClassEntity classEntity = classDao.selectById(classId.trim());
        if (null == classEntity) {
            return false;
        }

        if (null == personnelType
                || personnelType < Constants.TEACHER_HEAD
                || personnelType > Constants.TEACHER_INSTRUCTOR
                ) {
            personnelType = Constants.TEACHER_HEAD;
        }

        //新建人员班级关系信息
        ClassTeacherEntity classTeacher = new ClassTeacherEntity();

        classTeacher.setId(ResourceUtils.getUUID());
        classTeacher.setClassId(classId);
        classTeacher.setPersonnelId(personnelId);
        classTeacher.setPersonnelType(personnelType);
        classTeacher.setTeacherIntroduction(teacherIntroduction);

        Integer updateNum = classTeacherDao.insert(classTeacher);
        if (!CheckDataUtils.checkAddNum(updateNum)) {
            return false;
        }

        return true;
    }
}
